package com.leetcode.algorithm.y22.m07;

/**
 * 28. 实现 strStr()
 * 
 * https://leetcode.cn/problems/implement-strstr/
 * 
 * @author jie.deng
 *
 */
class Question0028Solution01 {
	
	public int strStr(String haystack, String needle) {
		int len1 = haystack.length();
		int len2 = needle.length();
		if (len1 < len2) {
			return -1;
		}

		for (int i = 0; i + len2 - 1 < len1; i++) {
			if (haystack.charAt(i) == needle.charAt(0)) {
				if (needle.equals(haystack.substring(i, i + len2))) {
					return i;
				}
			}
		}
		return -1;
	}
    
}